/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectAnggota.java
 *
 * Created on 11 Jan 13, 9:06:13
 */
package sistemperpustakaan.view.transaksi;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import sistemperpustakaan.controller.AnggotaController;
import sistemperpustakaan.controller.TransaksiController;
import sistemperpustakaan.entity.ViewAnggota;
import sistemperpustakaan.model.AnggotaModel;

/**
 *
 * @author EKo
 */
public class SelectAnggota extends javax.swing.JDialog implements ListSelectionListener {

    AnggotaModel model;
    SelectAnggotaTableModel tableModel;
    TransaksiController controller;
    int idAnggota = -1;

    /** Creates new form SelectAnggota */
    public SelectAnggota(java.awt.Frame parent, boolean modal, TransaksiController controller) {
        super(parent, modal);
        initComponents();
        this.controller = controller;
        model = new AnggotaModel();
//        find(txtCari.getText());
        tableModel = new SelectAnggotaTableModel(model.getListAnggota(0,"", txtCari.getText(), ""));
        tableAnggota.setModel(tableModel);
        setAction();
        tableAnggota.getSelectionModel().addListSelectionListener(this);
    }

    public final void setAction() {
        buttonFind.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                find(txtCari.getText());
            }
        });
        buttonCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        buttonSelect.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (idAnggota > 0) {
                    controller.setSelectedAnggota(idAnggota);
                    dispose();
                }
            }
        });
    }

    protected final void find(String nama) {
        tableModel = new SelectAnggotaTableModel(model.getListAnggota(-1, txtNis.getText(), txtCari.getText(), ""));
        tableAnggota.setModel(tableModel);
        tableAnggota.getColumnModel().getColumn(1).setPreferredWidth(120);
        tableAnggota.getColumnModel().getColumn(2).setPreferredWidth(150);
        tableAnggota.getColumnModel().getColumn(3).setPreferredWidth(150);
        tableAnggota.getColumnModel().getColumn(4).setPreferredWidth(150);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCari = new javax.swing.JTextField();
        buttonFind = new javax.swing.JButton();
        buttonSelect = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableAnggota = new javax.swing.JTable();
        txtNis = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Cari Nama : ");

        buttonFind.setText("CARI");

        buttonSelect.setText("Select");

        buttonCancel.setText("Cancel");

        tableAnggota.setAutoCreateRowSorter(true);
        tableAnggota.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableAnggota.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tableAnggota);

        jLabel2.setText("Cari NIS/NIK");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(330, Short.MAX_VALUE)
                        .addComponent(buttonSelect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNis, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22)
                        .addComponent(buttonFind, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonFind)
                    .addComponent(txtNis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSelect)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonFind;
    private javax.swing.JButton buttonSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableAnggota;
    private javax.swing.JTextField txtCari;
    private javax.swing.JTextField txtNis;
    // End of variables declaration//GEN-END:variables

    public void valueChanged(ListSelectionEvent e) {
        try {
            idAnggota = Integer.valueOf(tableAnggota.getValueAt(tableAnggota.getSelectedRow(), 0).toString());
        } catch (Exception ex) {
        }
    }
}

class SelectAnggotaTableModel extends AbstractTableModel {

    List<ViewAnggota> listAnggota;
    private String[] header = {"ID Anggota", "NIS/NIK","Type", "Nama", "Alamat"};

    public SelectAnggotaTableModel(List<ViewAnggota> listAnggota) {
        this.listAnggota = listAnggota;
    }

    public int getRowCount() {
        return listAnggota.size();
    }

    public int getColumnCount() {
        return 5;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return listAnggota.get(rowIndex).getId();
            case 1:
                return listAnggota.get(rowIndex).getNis_nik();
            case 2:
                return listAnggota.get(rowIndex).getType();
            case 3:
                return listAnggota.get(rowIndex).getNama();
            case 4:
                return listAnggota.get(rowIndex).getAlamat();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        return header[column];
    }
}
